//
//  BK_OD_LimitBuy_View.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/3/3.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_OD_LimitBuy_View.h"
#import "BK_OD_LimitBuy_Cell.h"
#import "BK_OD_Color.h"
/**
 限购详情  view
 */
@implementation BK_OD_LimitBuy_View
{
    UITableView *_table;
    UILabel *_title;
    UIButton *_exit;
    NSArray *_dataSource;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    
    _table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.backgroundColor = kOrderWhite;
    _table.delegate = self;
    _table.dataSource = self;
    [self addSubview:_table];
    
    _title = [[UILabel alloc] init];
    _title.backgroundColor = kOrderWhite;
    _title.textAlignment = NSTextAlignmentCenter;
    _title.font = UIFontCustom(14);
    _title.text = @"限购详情";
    [self addSubview:_title];
    
    _exit = [UIButton buttonWithType:UIButtonTypeCustom];
    [_exit setImage:[UIImage imageNamed:@"BKOrder_exit"] forState:UIControlStateNormal];
    [_exit addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_exit];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(300);
    }];
    
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(_table.mas_top);
        make.height.mas_equalTo(40);
    }];
    
    [_exit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self);
        make.centerY.mas_equalTo(_title);
        make.height.width.mas_equalTo(_title.mas_height);
    }];
}

- (void)setLimitBuyData:(NSDictionary *)limitBuyData
{
    _limitBuyData = limitBuyData;
    [self creatData];
}

- (void)creatData
{
    NSArray  *limitArray = _limitBuyData[@"membershipPurchaseistV1"];
    
    NSMutableArray *sectionArr = [NSMutableArray array];
    NSMutableArray *cellArr = [NSMutableArray array];
    int index = 0;
    for (NSDictionary *details in limitArray)
    {
        CellModel *cm = [[CellModel alloc] init];
        cm.className = NSStringFromClass([BK_OD_LimitBuy_Cell class]);
        cm.reuseIdentifier = [NSString stringWithFormat:@"limitBuyCell_%d",index];
        cm.height = [BK_OD_LimitBuy_Cell computeHeight:nil];
        cm.userInfo = details;
        index++;
        [cellArr addObject:cm];
    }
    
    SectionModel *sm = [SectionModel sectionModelWithTitle:nil cells:cellArr.copy];
    sm.headerhHeight = 0.1;
    sm.footerHeight = 0.1;
    
    [sectionArr addObject:sm];
    
    _dataSource = sectionArr.copy;
    [_table reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)exit
{
    [self dismiss];
}


//table  delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SectionModel *sm = _dataSource[section];
    return sm.cells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SectionModel *sm = _dataSource[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    BK_OD_Cell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    SectionModel *sm = _dataSource[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    BK_OD_Cell *dCell = (BK_OD_Cell *)cell;
    SEL selector = @selector(fillData:);
    
    if ([dCell respondsToSelector:selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Warc-performSelector-leaks"
        [dCell performSelector:selector withObject:cm.userInfo];
#pragma clang diagnostic pop
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    SectionModel *sm = _dataSource[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    SectionModel *sm = _dataSource[section];
    return sm.footerHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SectionModel *sm = _dataSource[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}

@end
